<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;
use think\Db;
use app\admin\model\TripPrice as TripPriceModel;
class Tripprice extends User
{

    /**
     * 价格类型列表
     */
    public function index()
    {
        $where['pid'] = 0;
        $trip_price_info = Db::name('trip_price')->where($where)->select();
        $pid_arr = array();
        foreach ($trip_price_info as $k=>$v) {
            if($v['updatetime'] && $v['updatetime']>0){
                $trip_price_info[$k]['updatetime'] = date("Y-m-d H:i:s",$v['updatetime']);
            }else{
                $trip_price_info[$k]['updatetime'] = "-";
            }
            if($v['creattime'] && $v['creattime']>0){
                $trip_price_info[$k]['creattime'] = date("Y-m-d H:i:s",$v['creattime']);
            }else{
                $trip_price_info[$k]['creattime'] = "-";
            }
            $trip_price_info[$k]['childprice'] = "";
            $pid_arr[$v['price_type_id']] = '';
        }

        $where1['pid'] = array("GT",0);
        $trip_price_info1 = Db::name('trip_price')->where($where1)->select();
        foreach ($trip_price_info1 as $k1=>$v1) {
            $pid = $v1['pid'];
            if(isset($pid_arr[$pid])){
                $pid_arr[$pid] .= $v1['type_name'].",";
            }
        }

        foreach ($pid_arr as $k_p=>$v_p) {
            if($v_p!==''){
                $v_p = substr($v_p,0,strlen($v_p)-1);
                foreach ($trip_price_info as $price_key=>$price_value) {
                    if($price_value['price_type_id']==$k_p){
                        $trip_price_info[$price_key]['childprice'] = $v_p;
                    }
                }
            }
        }

        $this->assign('info', $trip_price_info);
        return $this->fetch();
    }



    /**
     * 新增、修改价格类型
     */
    public function addtype()
    {
        $model = new TripPriceModel();
        $id = $this->request->has('price_type_id') ? $this->request->param('price_type_id', 0, 'intval') : 0;
        if ($id > 0) {
            //是修改操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();

                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['type_name', 'require', '名称不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }

                $post['updatetime']=time();

                if (false == $model->allowField(true)->save($post, ['price_type_id' => $id])) {
                    return $this->error('修改价格类型失败');
                } else {

                    return $this->success('修改价格类型成功', 'admin/tripprice/index');
                }
            } else {
                //非提交操作
                $info = $model->where('price_type_id', $id)->find();
                $this->assign('info', $info);
                return $this->fetch();
            }
        } else {
            //是新增操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();

                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['type_name', 'require', '名称不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }

                //验证名称是否存在
                $name = $model->where('type_name', $post['type_name'])->select();

                if (!empty($name)) {
                    return $this->error('提交失败：价格类型名称已存在');
                }

                $post['creattime']=time();
                $post['updatetime']=time();

                if (false == $model->allowField(true)->save($post)) {
                    return $this->error('添加价格类型失败');
                } else {
                    return $this->success('添加价格类型成功', 'admin/tripprice/index');
                }
            } else {
                //非提交操作
                return $this->fetch();
            }
        }
    }



    /**
     * 禁用/启用价格类型
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $model = new TripPriceModel();
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            $status = $this->request->has('status') ? $this->request->param('status', 0, 'intval') : 0;
            if($status){
                $data['type_status'] =$status;
            }
            if($status==2){
                $msg='禁用';
            }else{
                $msg='启用';
            }
            $data['updatetime']=time();
            if (false == $model->allowField(true)->save($data, ['price_type_id' => $id])) {
                return $this->error($msg.'失败');
            } else {
                $where['pid'] = $id;
                Db::name('trip_price')->where($where)->update($data);
                return $this->success($msg.'价格类型成功', 'admin/tripprice/index');
            }
        }
    }


    /**
     * 二级配置
     */
    public function addchild()
    {
        $id = $this->request->has('price_type_id') ? $this->request->param('price_type_id', 0, 'intval') : 0;
        if ($id > 0) {
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $post_arr = json_decode($post['post_arr'],true);
                $price_type_id = $post['price_type_id'];

                //删除post传来的id以外的数据
                $id_arr = array();
                foreach ($post_arr as $id_v){
                    $id_arr[] = $id_v[0];
                }
                $id_str = implode(",",$id_arr);
                $del_where['price_type_id'] = array('not in',$id_str);
                $del_where['pid'] = $price_type_id;
                Db::name('trip_price')->where($del_where)->delete();

                foreach ($post_arr as $arr_k=>$arr_v){
                    if($arr_v[0] > 0){//修改
                        $where_update['price_type_id'] = $arr_v[0];
                        $data=[
                            'type_name'=>$arr_v[1],
                            'value'=>$arr_v[2],
                            'updatetime'=>time(),
                        ];
                        Db::name('trip_price')->where($where_update)->update($data);
                    }else{//新增
                        $data=[
                            'pid'=>$price_type_id,
                            'type_name'=>$arr_v[1],
                            'value'=>$arr_v[2],
                            'type_status'=>1,
                            'creattime'=>time(),
                            'updatetime'=>time(),
                        ];
                        Db::name('trip_price')->insert($data);
                    }
                }

                $res['msg']='配置成功';
                $res['code']=1;
                return $res;
            }else{
                //展示
                $where['pid'] = $id;
                $child_info = Db::name('trip_price')->where($where)->select();

                $this->assign('price_type_id', $id);
                $this->assign('child_info', $child_info);
                return $this->fetch();
            }
        }
    }






}
